package com.xci.platform.auth.controller;

import com.xci.platform.annotation.Authorize;
import com.xci.platform.auth.entity.ReleaseHistoryEntity;
import com.xci.platform.auth.service.ReleaseHistoryService;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 系统发布记录控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/release-history")
public class ReleaseHistoryController extends AuthBaseController {

    /**
     * 系统发布记录服务
     */
    @Resource
    private ReleaseHistoryService releaseHistoryService;

    //region 页面视图

    /**
     * 系统发布记录首页
     */
    @GetMapping
    @Authorize(code = "SystemReleaseHistory")
    public String index() {
        return "auth/release-history/index";
    }

    /**
     * 系统发布记录新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemReleaseHistory.Create")
    public String create(ModelMap map) {
        createMark(map);
        ReleaseHistoryEntity entity = new ReleaseHistoryEntity();
        entity.setId(StringHelper.guid());
        map.put("entity", entity);
        return "auth/release-history/edit";
    }

    /**
     * 系统发布记录编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemReleaseHistory.Edit")
    public String edit(String id, ModelMap map) {
        ReleaseHistoryEntity entity = releaseHistoryService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/release-history/edit";
    }

    /**
     * 系统发布记录详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        ReleaseHistoryEntity entity = releaseHistoryService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        return "auth/release-history/details";
    }

    //endregion

    //region 数据处理

    /**
     * 系统发布记录表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemReleaseHistory.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(releaseHistoryService.queryPage(params));
    }

    /**
     * 保存新增系统发布记录
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemReleaseHistory.Create")
    public BoolMessage createSave(@ModelAttribute ReleaseHistoryEntity entity ) {
        return releaseHistoryService.save(entity, true);
    }

    /**
     * 保存修改系统发布记录
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemReleaseHistory.Edit")
    public BoolMessage editSave(@ModelAttribute ReleaseHistoryEntity entity ) {
        return releaseHistoryService.save(entity, false);
    }

    /**
     * 删除系统发布记录
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemReleaseHistory.Delete")
    public BoolMessage delete(String ids) {
        for (String id : StringHelper.splitToArray(ids)) {
            releaseHistoryService.delete(id);
        }
        return BoolMessage.True;
    }

    //endregion
}
